package solutions.ferpa.ztest.intf;

import br.com.telescope.adsrt.dom.DomDateTime;
import br.com.telescope.adsrt.dom.DomId;
import br.com.telescope.adsrt.dom.DomInteger;
import br.com.telescope.adsrt.dom.DomText;
import br.com.telescope.adsrt.dom.DomUpper;
import br.com.telescope.adsrt.lib.AdsException;
import br.com.telescope.adsrt.lib.Field;
import br.com.telescope.adsrt.lib.Form;
import br.com.telescope.sql.ModelQuery;
import br.com.telescope.t2.EntityBinding;
import br.com.telescope.t2.ServiceModel;
import br.com.telescope.t2.feature.DeleteFeature;
import br.com.telescope.t2.feature.InsertFeature;
import br.com.telescope.t2.feature.ListFeature;
import br.com.telescope.t2.feature.LookupFeature;
import br.com.telescope.t2.feature.UpdateFeature;
import br.com.telescope.t2.feature.ViewFeature;
import br.com.telescope.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
import solutions.ferpa.ztest.ent.*;

/**
 * Hidrômetros.
 * <p>
 * Interface para manutenção de Hidrômetro
 * <P>
 * Telescope origin: ZTEST.CAD_HIDROMETRO
 * <P>
 * Base entity: ZTEST.HIDROMETRO
 * <P>
 * @author TELESCOPE - Build task T2 Web Interface Generator
 * <P>
 * @version 0.1
 */
public class CadHidrometro extends ServiceModel {
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    public static final String GLOBAL_NAME = "ZTEST.CAD_HIDROMETRO";
    
    @Override
    public String globalName() {
        return GLOBAL_NAME;
    }
    
    {
        setTitle("Hidrômetros");
    }
    
    protected EntHidrometro base;
    protected EntModeloHidrometro modl;
    protected EntHidrometro rec; // NEED_TJW1_COMPATIBILITY
    
    // Service parameters
    private DomId pIdModeloHidrometro;
    
    // Record items
    private DomText aNumeroSerie;
    private DomDateTime aDataRegistro;
    private DomText aCargaNumerica;
    private DomId aModeloHidrometroId;
    private DomId aId;
    private DomInteger aNumeroLacre;
    private DomId aEnsaioBancadaId;
    private DomUpper aModlCodigo;
    private DomUpper aModlMarca;
    private DomText aModlDescricao;
    
    // Query items
    
    @Override
    public String getBaseSql() {
        return "select BASE.*"
                + " , MODL.CODIGO as MODL_CODIGO"
                + " , MODL.MARCA as MODL_MARCA"
                + " , MODL.DESCRICAO as MODL_DESCRICAO"
                + " , MODL.ID as Z_MODL_ID"
                + " from HIDROMETRO BASE"
                + " join MODELO_HIDROMETRO MODL on MODL.ID = BASE.MODELO_HIDROMETRO_ID";
    }
    
    @Override
    public void initAttributes() {
        
        aNumeroSerie = new DomText();
        aNumeroSerie.context(ctx).name("NUMERO_SERIE").label("Número de série do hidrômetro");
        
        aDataRegistro = new DomDateTime();
        aDataRegistro.context(ctx).name("DATA_REGISTRO").label("Data do registro");
        aDataRegistro.setDefaultValue("[SYSDATE]");
        
        aCargaNumerica = new DomText();
        aCargaNumerica.context(ctx).name("CARGA_NUMERICA").label("Carga numérica");
        
        aModeloHidrometroId = new DomId();
        aModeloHidrometroId.context(ctx).name("MODELO_HIDROMETRO_ID").label("Modelo do hidrômetro");
        
        aId = new DomId();
        aId.context(ctx).name("ID").label("Id");
        
        aNumeroLacre = new DomInteger();
        aNumeroLacre.context(ctx).name("NUMERO_LACRE").label("Número do lacre");
        
        aEnsaioBancadaId = new DomId();
        aEnsaioBancadaId.context(ctx).name("ENSAIO_BANCADA_ID").label("Ensaio (origem)");
        
        aModlCodigo = new DomUpper();
        aModlCodigo.context(ctx).name("MODL_CODIGO").label("Código do modelo de hidrômetro");
        aModlCodigo.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT CODIGO as valor"
                + " , CODIGO as pesquisar"
                + " , CODIGO as descricao"
                + " from MODELO_HIDROMETRO MODL)", "q"));
        aModlCodigo.setAutocompleteLength(2);
        
        aModlMarca = new DomUpper();
        aModlMarca.context(ctx).name("MODL_MARCA").label("Marca");
        aModlMarca.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT MARCA as valor"
                + " , MARCA as pesquisar"
                + " , MARCA as descricao"
                + " from MODELO_HIDROMETRO MODL)", "q"));
        aModlMarca.setAutocompleteLength(2);
        aModlMarca.setDefaultValue("ZENNER");
        
        aModlDescricao = new DomText();
        aModlDescricao.context(ctx).name("MODL_DESCRICAO").label("Descrição do hidrômetro");
        aModlDescricao.setListOfValuesQuery(createQuery()
                .select("*")
                .from("(SELECT DESCRICAO as valor"
                + " , DESCRICAO as pesquisar"
                + " , DESCRICAO as descricao"
                + " from MODELO_HIDROMETRO MODL)", "q"));
        aModlDescricao.setAutocompleteLength(2);
        
    }
    
    @Override
    public void initParameters() {
        pIdModeloHidrometro = new DomId(ctx, "ID_MODELO_HIDROMETRO", "Modelo do hidrômetro");
        pIdModeloHidrometro.setValue(ctx.getParameter("P_ID_MODELO_HIDROMETRO"));
        pIdModeloHidrometro.setSqlExpression("MODELO_HIDROMETRO_ID");
        if (pIdModeloHidrometro.isNotNull()) {
            aModeloHidrometroId.setValue(pIdModeloHidrometro.getValue());
        }
    }
    
    @Override
    public List<Field> getParameters() {
        if (allParameters == null) {
            allParameters = new ArrayList<Field>();
            allParameters.add(pIdModeloHidrometro);
        }
        return allParameters;
    }
    
    @Override
    public void copyBindParameters() {
        EntHidrometro record = getRecord();
        if (pIdModeloHidrometro.isNotNull()) {
            record.setModeloHidrometroId(pIdModeloHidrometro.getValue());
        }
    }
    
    @Override
    public void retrieveBindParameters() {
        pIdModeloHidrometro.setValue(getRecord().valueOfModeloHidrometroId());
    }
    
    @Override
    public void initEntityBindings() {
        EntityBinding binding;
        
        binding = new EntityBinding("ZTEST.MODELO_HIDROMETRO", "MODL", "Z_MODL_ID", "ZTEST.CAD_MODELO_HIDROMETRO.VIEW");
        binding.addFields(aModlCodigo, aModlMarca, aModlDescricao);
        entityBindings.add(binding);
    }
    
    @Override
    public String getQueryPkColumn() {
        return "BASE.ID";
    }
    
    @Override
    public void setId(String value) {
        this.aId.setValue(value);
    }
    
    @Override
    public EntHidrometro getRecord() {
        if (base == null) {
            base = new EntHidrometro(ctx);
            rec = base;
        }
        return this.base;
    }
    
    @Override
    public String getContextName() {
        return aNumeroSerie.toText();
    }
    
    @Override
    public String getDefaultOrderBy() {
        return "NUMERO_SERIE, DATA_REGISTRO, MODL_CODIGO";
    }
    
    @Override
    public void translateInsertFks(Form form) {
        ModelQuery query;
        
        // MODELO_HIDROMETRO_ID
        try {
            if (form.hasFields("MODL_CODIGO", "MODL_MARCA", "MODL_DESCRICAO")) {
                query = createQuery()
                        .select("MODL.ID")
                        .from("MODELO_HIDROMETRO", "MODL")
                        .filter("MODL.CODIGO", form.get("MODL_CODIGO"))
                        .filter("MODL.MARCA", form.get("MODL_MARCA"))
                        .filter("MODL.DESCRICAO", form.get("MODL_DESCRICAO"))
                        ;
                base.setModeloHidrometroId(ObjectUtil.toLong(translateFk(query)));
            }
        } catch (AdsException ex) {
            throw new RuntimeException(ex);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="LIST">
    public class FList extends ListFeature {
        
        @Override
        public void initForm() {
            addField(aNumeroSerie, "view");
            addField(aDataRegistro, "view");
            addField(aModlCodigo, "view");
        }
        
        {
            setFeatureName("LIST");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="VIEW">
    public class FView extends ViewFeature {
        
        @Override
        public void initForm() {
            addField(aNumeroSerie, "view");
            addField(aDataRegistro, "view");
            addField(aModlCodigo, "view");
            addField(aModlMarca, "view");
            addField(aModlDescricao, "view");
        }
        
        {
            setFeatureName("VIEW");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="UPDATE">
    public class FUpdate extends UpdateFeature {
        
        @Override
        public void initForm() {
            aNumeroSerie.setEditable(false);
            addField(aNumeroSerie, "view");
            aDataRegistro.setEditable(false);
            addField(aDataRegistro, "view");
            aModlCodigo.setEditable(false);
            addField(aModlCodigo, "view");
            aModlMarca.setEditable(false);
            addField(aModlMarca, "view");
            aModlDescricao.setEditable(false);
            addField(aModlDescricao, "view");
        }
        
        {
            setFeatureName("UPDATE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="DELETE">
    public class FDelete extends DeleteFeature {
        
        @Override
        public void initForm() {
            addField(aNumeroSerie, "view");
            addField(aDataRegistro, "view");
            addField(aModlCodigo, "view");
        }
        
        {
            setFeatureName("DELETE");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="INSERT">
    public class FInsert extends InsertFeature {
        
        @Override
        public void initForm() {
            aNumeroSerie.setOptional(false);
            addField(aNumeroSerie, "rqd");
            aDataRegistro.setOptional(false);
            addField(aDataRegistro, "rqd");
            aModlCodigo.setOptional(false);
            addField(aModlCodigo, "rqd");
            aModlMarca.setOptional(false);
            addField(aModlMarca, "rqd");
            aModlDescricao.setOptional(false);
            addField(aModlDescricao, "rqd");
        }
        
        {
            setFeatureName("INSERT");
            setPrimary(false);
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="MODL_CODIGO_LOOKUP">
    public class FModlCodigoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aModlCodigo.setEditable(true);
            addField(aModlCodigo, "opt");
            aModlMarca.setEditable(true);
            addField(aModlMarca, "opt");
            aModlDescricao.setEditable(true);
            addField(aModlDescricao, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("MODL.CODIGO as MODL_CODIGO")
                    .select("MODL.MARCA as MODL_MARCA")
                    .select("MODL.DESCRICAO as MODL_DESCRICAO")
                    .from("MODELO_HIDROMETRO", "MODL")
                    ;
        }
        
        {
            setFeatureName("MODL_CODIGO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="MODL_DESCRICAO_LOOKUP">
    public class FModlDescricaoLookup extends LookupFeature {
        
        @Override
        public void initForm() {
            aModlCodigo.setEditable(true);
            addField(aModlCodigo, "opt");
            aModlMarca.setEditable(true);
            addField(aModlMarca, "opt");
            aModlDescricao.setEditable(true);
            addField(aModlDescricao, "opt");
        }
        
        @Override
        public ModelQuery getLookupQuery() {
            return createQuery()
                    .select("MODL.CODIGO as MODL_CODIGO")
                    .select("MODL.MARCA as MODL_MARCA")
                    .select("MODL.DESCRICAO as MODL_DESCRICAO")
                    .from("MODELO_HIDROMETRO", "MODL")
                    ;
        }
        
        {
            setFeatureName("MODL_DESCRICAO_LOOKUP");
        }
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="initFeatures">
    
    @Override
    protected void initFeatures() {
        addFeature(new FList());
        addFeature(new FView());
        addFeature(new FUpdate());
        addFeature(new FDelete());
        addFeature(new FInsert());
        addFeature(new FModlCodigoLookup());
        addFeature(new FModlDescricaoLookup());
        super.initFeatures();
    }
    
    @Override
    public String getDefaultFeature() {
        return "LIST";
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    
    public String getNumeroSerie() {
        return aNumeroSerie.getValue();
    }
    
    public void setNumeroSerie(String value) {
        this.aNumeroSerie.setValue(value);
    }
    
    public java.sql.Timestamp getDataRegistro() {
        return aDataRegistro.getValue();
    }
    
    public void setDataRegistro(java.sql.Timestamp value) {
        this.aDataRegistro.setValue(value);
    }
    
    public String getCargaNumerica() {
        return aCargaNumerica.getValue();
    }
    
    public void setCargaNumerica(String value) {
        this.aCargaNumerica.setValue(value);
    }
    
    public Long getModeloHidrometroId() {
        return aModeloHidrometroId.getValue();
    }
    
    public void setModeloHidrometroId(Long value) {
        this.aModeloHidrometroId.setValue(value);
    }
    
    @Override
    public Long getId() {
        return aId.getValue();
    }
    
    public void setId(Long value) {
        this.aId.setValue(value);
    }
    
    public Integer getNumeroLacre() {
        return aNumeroLacre.getValue();
    }
    
    public void setNumeroLacre(Integer value) {
        this.aNumeroLacre.setValue(value);
    }
    
    public Long getEnsaioBancadaId() {
        return aEnsaioBancadaId.getValue();
    }
    
    public void setEnsaioBancadaId(Long value) {
        this.aEnsaioBancadaId.setValue(value);
    }
    
    public String getModlCodigo() {
        return aModlCodigo.getValue();
    }
    
    public void setModlCodigo(String value) {
        this.aModlCodigo.setValue(value);
    }
    
    public String getModlMarca() {
        return aModlMarca.getValue();
    }
    
    public void setModlMarca(String value) {
        this.aModlMarca.setValue(value);
    }
    
    public String getModlDescricao() {
        return aModlDescricao.getValue();
    }
    
    public void setModlDescricao(String value) {
        this.aModlDescricao.setValue(value);
    }
    
    public Long getParameterIdModeloHidrometro() {
        return pIdModeloHidrometro.getValue();
    }
    
    public void setParameterIdModeloHidrometro(Long value) {
        pIdModeloHidrometro.setValue(value);
        aModeloHidrometroId.setValue(value);
    }
    // </editor-fold>
    
    
    @Override
    public String getJsServiceFile() {
        return "ui/ztest/cad_hidrometro.js";
    }
}
